import random
import time
hitlist=["用堪比Henry McCarty的拔枪速度掏出凶器命中了","朝天空乱射，没想到却撞大运射中了","画了个圈圈诅咒","通过可耻的海豹行为，成功气炸了","运用枪斗术改变弹道击中了"]
misslist=["射击时脚踩香蕉皮完成了数个高难度动作后以平沙落雁式迫降","对面展开AT力场，使攻击无效化","敌方阿利斯塔附身，不拿盾牌都开了盾墙","对面偷改游戏公式成功，成功躲过攻击","The Force be with your enemy"]
class Character:
    playerlist=[]
    def __init__(self,name,sex,HP,AC,count):
        self.name=name
        self.sex=sex
        self.HP=int(HP)
        self.AC=int(AC)
        self.id=count
        self.status="Free"
        Character.playerlist.append(self.id)
class Room:
    roomlist=[]
    def __init__(self,id):
        self.id=id
        self.pcount=0
        self.roomplayerlist=[]
        Room.roomlist.append(id)

    def inroom(self,id):
        self.pcount+=1
        self.roomplayerlist.append(id)
        names['P%s' % id].status="Waiting"
        if self.pcount==2:
            self.gamestart()
            names['P%s' % self.roomplayerlist[0]].status="Free"
            names['P%s' % self.roomplayerlist[1]].status="Free"
            self.roomplayerlist.clear()

    def outroom(self,id):
        if id in self.roomplayerlist:
            self.pcount-=1
            names['P%s' % id].status="Free"
        else:
            raise ValueError
        self.roomplayerlist.remove(id)

    def gamestart(self):
        while True:
            dice=random.randint(1,20)
            print("{0}的回合：".format(names['P%s' % self.roomplayerlist[0]].name))
            time.sleep(1)
            if dice > 8:
                if int(names['P%s' % self.roomplayerlist[1]].AC) == 0:
                    names['P%s' % self.roomplayerlist[1]].HP -= 10
                    print("{0} {1} {2} \n{3} - 10 HP".format(names['P%s' % self.roomplayerlist[0]].name,random.choice(hitlist),names['P%s' % self.roomplayerlist[1]].name,names['P%s' % self.roomplayerlist[1]].name))
                else:
                    names['P%s' % self.roomplayerlist[1]].HP -= 5
                    print("{0} {1} {2} \n{3} - 5 HP".format(names['P%s' % self.roomplayerlist[0]].name,random.choice(hitlist),names['P%s' % self.roomplayerlist[1]].name,names['P%s' % self.roomplayerlist[1]].name))
            else:
                print(random.choice(misslist))
            if names['P%s' % self.roomplayerlist[1]].HP <= 0:
                print("{0} WIN".format(names['P%s' % self.roomplayerlist[0]].name))
                break

            dice = random.randint(1,20)
            time.sleep(1)
            print("{0}的回合：".format(names['P%s' % self.roomplayerlist[1]].name))
            if dice > 8:
                if int(names['P%s' % self.roomplayerlist[0]].AC) == 0:
                    names['P%s' % self.roomplayerlist[0]].HP -= 10
                    print("{0} {1} {2} \n{3} - 10 HP".format(names['P%s' % self.roomplayerlist[1]].name,random.choice(hitlist),names['P%s' % self.roomplayerlist[0]].name,names['P%s' % self.roomplayerlist[0]].name))
                else:
                    names['P%s' % self.roomplayerlist[0]].HP -= 5
                    print("{0} {1} {2} \n{3} - 5 HP".format(names['P%s' % self.roomplayerlist[1]].name,random.choice(hitlist),names['P%s' % self.roomplayerlist[0]].name,names['P%s' % self.roomplayerlist[0]].name))
            else:
                print(random.choice(misslist))
            if names['P%s' % self.roomplayerlist[0]].HP <= 0:
                print("{0} WIN".format(names['P%s' % self.roomplayerlist[1]].name))
                break
def CreatePlayer():
    global count
    name=input("input name:")
    while True:
        sex=input("input sex:(like F or M)")
        if sex=="F" or sex=="M":
            break
        else:
            print("input error")
    HP=input("input HP:")
    try:
        int(HP)
        if int(HP)<=0:
            HP='100'
    except ValueError:
        HP='100'
    while True:
        AC=input("input AC level:")
        if AC=="1" or AC=="0":
            break
        else:
            print("input error")
    names['P%s'% count]=Character(name,sex,HP,AC,count)
    print("your id is P{0}".format(count))
    count+=1
def CreateRoom():
    global rcount
    rnames['R%s'% rcount]=Room(rcount)
    print("Room id is R{0}".format(rcount))
    rcount+=1
def JoinRoom(uid,rid):
    if uid not in Character.playerlist or rid not in Room.roomlist:
        print("ID ERROR")
        return 0
    if names['P%s' % uid].status=='Waiting':
        print("{0} in another rooom" .format(names['P%s'% uid].name))
        return 0
    rnames["R%s" % rid].inroom(uid)
names=locals()
count=0
rnames=locals()
rcount=0

P0=Character('TEST','F','30','1',count)
P1=Character('T1','F','30','1',1)
CreateRoom()
print(P0.status)
JoinRoom(0,0)
print(P0.status)
JoinRoom(0,0)
JoinRoom(1,0)
print(Character.playerlist)
print(P0.status)
print(P1.status)
print(R0.roomplayerlist)



